package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.NavDao;
import com.cn.wanxi.dao.NewsDao;
import com.cn.wanxi.model.NewsModel;
import com.cn.wanxi.service.INewsService;
import com.cn.wanxi.util.IOTool;
import com.cn.wanxi.util.Path;
import com.cn.wanxi.util.StringTool;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class NewsThreeServiceImpl implements INewsService {

//    private static final String SPLIT = "\n";
//    String diretory = "note";
//    String fileName = "newsForHomePage.txt";
    @Override
    public NewsModel[] getNewsModelsForHomePage(String path) {
        //        1.创建文件
//        File file = IOTool.createNote(path, diretory, fileName);
//        2.写入数据
        List<String> data = getData();
        IOTool.writeNote(data, path, Path.FILE_NAME_NEWS_FOR_HOME_PAGE);
//        3.读取数据
        String result = IOTool.readNote();
//        4.封装数据
        //        5.返回数据
        return setData(result);
    }

    private NewsModel[] setData(String result) {
        String[] results = result.split(Path.SPLIT);
        String title = StringTool.replace(results[0]);
        String content = StringTool.replace(results[1]);
        String[] titles = title.split(", ");
        String[] contents = content.split(", ");
        int length = titles.length;
        NewsModel[] newsModels = new NewsModel[length];
        for (int i = 0; i < length; i++) {
            NewsModel model = new NewsModel();
//            model.setId(newsDao.getIdsForHomePage()[i]);
            model.setTitle(titles[i]);
            model.setContent(contents[i]);
            newsModels[i] = model;
        }
        return newsModels;
    }

    private List<String> getData() {
        List<String> list = new ArrayList<>();
        NewsDao newsDao = new NewsDao();
        list.add(Arrays.toString(newsDao.getTitlesForHomePage()));
        list.add(Arrays.toString(newsDao.getContentsForHomePage()));
        return list;
    }
}
